package com.breeze.blockchain.socket.stomp;

import cn.hutool.http.HtmlUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

/**
 * stomp
 *
 * @author breeze
 */
@RestController
@Slf4j
public class WebSocketStompController {

  @Resource SimpMessagingTemplate simpMessagingTemplate;

  @MessageMapping("/hello")
  @SendTo("/topic/greetings")
  public Greeting greeting(HelloMessage message) throws Exception {
    Thread.sleep(1000);
    log.info("Hello, {}!", HtmlUtils.htmlEscape(message.getName()));
    return new Greeting("Hello, " + HtmlUtil.escape(message.getName()) + "!");
  }

  @GetMapping("/sendTo")
  public void greeting3(HelloMessage message) throws Exception {
    Thread.sleep(1000);
    simpMessagingTemplate.convertAndSend(
        "/topic/greetings",
        new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!"));
  }
}
